# -*- coding: utf-8 -*-
import os
import glob, sys
import datetime as dt
import pandas as pd
import numpy as np

# This script finds the most common fuel in each fire area, and assigns the corresponding ROS value.

fuel_number=int(sys.argv[1]) # fuel_number=1 for most common fuel, fuel_number=2 for second most common fuel, etc
experiment=str(sys.argv[2])
contribution=str(sys.argv[3])
radio = int(sys.argv[5])

fuel_name = ['GR2', 'GR4', 'GR6', 'GR7', 'GR8',
             'GR9', 'SH2', 'SH3', 'SH5', 'SH7',
             'SH8', 'SH9', 'TU1', 'TU2', 'TU3',
             'TU5', 'TL3'] 

fuelmce = np.array([15., 15., 40., 15., 30.,       # GR2, GR4, GR6, GR7, GR8
                    40., 15., 40., 15., 15.,       # GR9, SH2, SH3, SH5, SH7
                    40., 40., 20., 30., 30.,       # SH8, SH9, TU1, TU2, TU3
                    25., 30.])                     # TU5, TL3 Moisture content of extinction (%)


fuels = pd.read_csv('../OUTS/Fuels_fires_d'+str(radio)+'.csv')

ros_GR2 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR2.csv')
ros_GR4 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR4.csv')
ros_GR6 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR6.csv')
ros_GR7 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR7.csv')
ros_GR8 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR8.csv')
ros_GR9 = pd.read_csv('./OUTS/Attribution_'+experiment+'_GR9.csv')
ros_SH2 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH2.csv')
ros_SH3 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH3.csv')
ros_SH5 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH5.csv')
ros_SH7 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH7.csv')
ros_SH8 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH8.csv')
ros_SH9 = pd.read_csv('./OUTS/Attribution_'+experiment+'_SH9.csv')
ros_TU1 = pd.read_csv('./OUTS/Attribution_'+experiment+'_TU1.csv')
ros_TU2 = pd.read_csv('./OUTS/Attribution_'+experiment+'_TU2.csv')
ros_TU3 = pd.read_csv('./OUTS/Attribution_'+experiment+'_TU3.csv')
ros_TU5 = pd.read_csv('./OUTS/Attribution_'+experiment+'_TU5.csv')
ros_TL3 = pd.read_csv('./OUTS/Attribution_'+experiment+'_TL3.csv')

Attribution=pd.DataFrame({})

F1 = np.array([fuels['GR2'],
	fuels['GR4'],
	fuels['GR6'],
	fuels['GR7'],
	fuels['GR8'],
	fuels['GR9'],
	fuels['SH2'],
	fuels['SH3'],
	fuels['SH5'],
	fuels['SH7'],
	fuels['SH8'],
	fuels['SH9'],
	fuels['TU1'],
	fuels['TU2'],
	fuels['TU3'],
	fuels['TU5'],
	fuels['TL3']])
F2 = F1.copy()
F2[F2==F1.max(axis=0)]=0.
F3 = F2.copy()
F3[F3==F2.max(axis=0)]=0.
F4 = F3.copy()
F4[F4==F3.max(axis=0)]=0.
F5 = F4.copy()
F5[F5==F4.max(axis=0)]=0.
F6 = F5.copy()
F6[F6==F5.max(axis=0)]=0.
F7 = F6.copy()
F7[F7==F6.max(axis=0)]=0.
F8 = F7.copy()
F8[F8==F7.max(axis=0)]=0.
F9 = F8.copy()
F9[F9==F8.max(axis=0)]=0.
F10 = F9.copy()
F10[F10==F9.max(axis=0)]=0.

F1[F1==F1.max(axis=0)]=1.
F1[F1!=F1.max(axis=0)]=0.
F2[F2==F2.max(axis=0)]=1.
F2[F2!=F2.max(axis=0)]=0.
F3[F3==F3.max(axis=0)]=1.
F3[F3!=F3.max(axis=0)]=0.
F4[F4==F4.max(axis=0)]=1.
F4[F4!=F4.max(axis=0)]=0.
F5[F5==F5.max(axis=0)]=1.
F5[F5!=F5.max(axis=0)]=0.
F6[F6==F6.max(axis=0)]=1.
F6[F6!=F6.max(axis=0)]=0.
F7[F7==F7.max(axis=0)]=1.
F7[F7!=F7.max(axis=0)]=0.
F8[F8==F8.max(axis=0)]=1.
F8[F8!=F8.max(axis=0)]=0.
F9[F9==F9.max(axis=0)]=1.
F9[F9!=F9.max(axis=0)]=0.
F10[F10==F10.max(axis=0)]=1.
F10[F10!=F10.max(axis=0)]=0.

fuel_selection = ['fuel0', 'fuel1', 'fuel2', 'fuel3', 'fuel4', 'fuel5', 'fuel6', 'fuel7', 'fuel8', 'fuel9', 'fuel10']
output_name = fuel_selection[fuel_number]
FUELs = dict({'fuel1':F1, 'fuel2':F2, 'fuel3':F3, 'fuel4':F4, 'fuel5':F5, 'fuel6':F6, 'fuel7':F7, 'fuel8':F8, 'fuel9':F9, 'fuel10':F10})
fuel = pd.DataFrame(data=np.transpose(FUELs[fuel_selection[fuel_number]]),columns=fuel_name)

fuel = fuel.reindex(list(range(0,1365)),fill_value=0)

ROS_obs = (ros_GR2['ROS_obs']*fuel['GR2']+
	ros_GR4['ROS_obs']*fuel['GR4']+
	ros_GR6['ROS_obs']*fuel['GR6']+
	ros_GR7['ROS_obs']*fuel['GR7']+
	ros_GR8['ROS_obs']*fuel['GR8']+
	ros_GR9['ROS_obs']*fuel['GR9']+
	ros_SH2['ROS_obs']*fuel['SH2']+
	ros_SH3['ROS_obs']*fuel['SH3']+
	ros_SH5['ROS_obs']*fuel['SH5']+
	ros_SH7['ROS_obs']*fuel['SH7']+
	ros_SH8['ROS_obs']*fuel['SH8']+
	ros_SH9['ROS_obs']*fuel['SH9']+
	ros_TU1['ROS_obs']*fuel['TU1']+
	ros_TU2['ROS_obs']*fuel['TU2']+
	ros_TU3['ROS_obs']*fuel['TU3']+
	ros_TU5['ROS_obs']*fuel['TU5']+
	ros_TL3['ROS_obs']*fuel['TL3'])

ROS_model1 = (ros_GR2['ROS_'+contribution+'_model1']*fuel['GR2']+
	ros_GR4['ROS_'+contribution+'_model1']*fuel['GR4']+
	ros_GR6['ROS_'+contribution+'_model1']*fuel['GR6']+
	ros_GR7['ROS_'+contribution+'_model1']*fuel['GR7']+
	ros_GR8['ROS_'+contribution+'_model1']*fuel['GR8']+
	ros_GR9['ROS_'+contribution+'_model1']*fuel['GR9']+
	ros_SH2['ROS_'+contribution+'_model1']*fuel['SH2']+
	ros_SH3['ROS_'+contribution+'_model1']*fuel['SH3']+
	ros_SH5['ROS_'+contribution+'_model1']*fuel['SH5']+
	ros_SH7['ROS_'+contribution+'_model1']*fuel['SH7']+
	ros_SH8['ROS_'+contribution+'_model1']*fuel['SH8']+
	ros_SH9['ROS_'+contribution+'_model1']*fuel['SH9']+
	ros_TU1['ROS_'+contribution+'_model1']*fuel['TU1']+
	ros_TU2['ROS_'+contribution+'_model1']*fuel['TU2']+
	ros_TU3['ROS_'+contribution+'_model1']*fuel['TU3']+
	ros_TU5['ROS_'+contribution+'_model1']*fuel['TU5']+
	ros_TL3['ROS_'+contribution+'_model1']*fuel['TL3'])

ROS_model2 = (ros_GR2['ROS_'+contribution+'_model2']*fuel['GR2']+
	ros_GR4['ROS_'+contribution+'_model2']*fuel['GR4']+
	ros_GR6['ROS_'+contribution+'_model2']*fuel['GR6']+
	ros_GR7['ROS_'+contribution+'_model2']*fuel['GR7']+
	ros_GR8['ROS_'+contribution+'_model2']*fuel['GR8']+
	ros_GR9['ROS_'+contribution+'_model2']*fuel['GR9']+
	ros_SH2['ROS_'+contribution+'_model2']*fuel['SH2']+
	ros_SH3['ROS_'+contribution+'_model2']*fuel['SH3']+
	ros_SH5['ROS_'+contribution+'_model2']*fuel['SH5']+
	ros_SH7['ROS_'+contribution+'_model2']*fuel['SH7']+
	ros_SH8['ROS_'+contribution+'_model2']*fuel['SH8']+
	ros_SH9['ROS_'+contribution+'_model2']*fuel['SH9']+
	ros_TU1['ROS_'+contribution+'_model2']*fuel['TU1']+
	ros_TU2['ROS_'+contribution+'_model2']*fuel['TU2']+
	ros_TU3['ROS_'+contribution+'_model2']*fuel['TU3']+
	ros_TU5['ROS_'+contribution+'_model2']*fuel['TU5']+
	ros_TL3['ROS_'+contribution+'_model2']*fuel['TL3'])

ROS_model3 = (ros_GR2['ROS_'+contribution+'_model3']*fuel['GR2']+
	ros_GR4['ROS_'+contribution+'_model3']*fuel['GR4']+
	ros_GR6['ROS_'+contribution+'_model3']*fuel['GR6']+
	ros_GR7['ROS_'+contribution+'_model3']*fuel['GR7']+
	ros_GR8['ROS_'+contribution+'_model3']*fuel['GR8']+
	ros_GR9['ROS_'+contribution+'_model3']*fuel['GR9']+
	ros_SH2['ROS_'+contribution+'_model3']*fuel['SH2']+
	ros_SH3['ROS_'+contribution+'_model3']*fuel['SH3']+
	ros_SH5['ROS_'+contribution+'_model3']*fuel['SH5']+
	ros_SH7['ROS_'+contribution+'_model3']*fuel['SH7']+
	ros_SH8['ROS_'+contribution+'_model3']*fuel['SH8']+
	ros_SH9['ROS_'+contribution+'_model3']*fuel['SH9']+
	ros_TU1['ROS_'+contribution+'_model3']*fuel['TU1']+
	ros_TU2['ROS_'+contribution+'_model3']*fuel['TU2']+
	ros_TU3['ROS_'+contribution+'_model3']*fuel['TU3']+
	ros_TU5['ROS_'+contribution+'_model3']*fuel['TU5']+
	ros_TL3['ROS_'+contribution+'_model3']*fuel['TL3'])

ROS_model4 = (ros_GR2['ROS_'+contribution+'_model4']*fuel['GR2']+
	ros_GR4['ROS_'+contribution+'_model4']*fuel['GR4']+
	ros_GR6['ROS_'+contribution+'_model4']*fuel['GR6']+
	ros_GR7['ROS_'+contribution+'_model4']*fuel['GR7']+
	ros_GR8['ROS_'+contribution+'_model4']*fuel['GR8']+
	ros_GR9['ROS_'+contribution+'_model4']*fuel['GR9']+
	ros_SH2['ROS_'+contribution+'_model4']*fuel['SH2']+
	ros_SH3['ROS_'+contribution+'_model4']*fuel['SH3']+
	ros_SH5['ROS_'+contribution+'_model4']*fuel['SH5']+
	ros_SH7['ROS_'+contribution+'_model4']*fuel['SH7']+
	ros_SH8['ROS_'+contribution+'_model4']*fuel['SH8']+
	ros_SH9['ROS_'+contribution+'_model4']*fuel['SH9']+
	ros_TU1['ROS_'+contribution+'_model4']*fuel['TU1']+
	ros_TU2['ROS_'+contribution+'_model4']*fuel['TU2']+
	ros_TU3['ROS_'+contribution+'_model4']*fuel['TU3']+
	ros_TU5['ROS_'+contribution+'_model4']*fuel['TU5']+
	ros_TL3['ROS_'+contribution+'_model4']*fuel['TL3'])

ROS_model5 = (ros_GR2['ROS_'+contribution+'_model5']*fuel['GR2']+
	ros_GR4['ROS_'+contribution+'_model5']*fuel['GR4']+
	ros_GR6['ROS_'+contribution+'_model5']*fuel['GR6']+
	ros_GR7['ROS_'+contribution+'_model5']*fuel['GR7']+
	ros_GR8['ROS_'+contribution+'_model5']*fuel['GR8']+
	ros_GR9['ROS_'+contribution+'_model5']*fuel['GR9']+
	ros_SH2['ROS_'+contribution+'_model5']*fuel['SH2']+
	ros_SH3['ROS_'+contribution+'_model5']*fuel['SH3']+
	ros_SH5['ROS_'+contribution+'_model5']*fuel['SH5']+
	ros_SH7['ROS_'+contribution+'_model5']*fuel['SH7']+
	ros_SH8['ROS_'+contribution+'_model5']*fuel['SH8']+
	ros_SH9['ROS_'+contribution+'_model5']*fuel['SH9']+
	ros_TU1['ROS_'+contribution+'_model5']*fuel['TU1']+
	ros_TU2['ROS_'+contribution+'_model5']*fuel['TU2']+
	ros_TU3['ROS_'+contribution+'_model5']*fuel['TU3']+
	ros_TU5['ROS_'+contribution+'_model5']*fuel['TU5']+
	ros_TL3['ROS_'+contribution+'_model5']*fuel['TL3'])

Attribution['Date']=fuels['Date']
Attribution['Municipality']=fuels['Municipaliy']
Attribution['Province']=fuels['Province']
Attribution['Area']=fuels['Area']
Attribution['ROS_obs']=ROS_obs
Attribution['ROS_model1']=ROS_model1
Attribution['ROS_model2']=ROS_model2
Attribution['ROS_model3']=ROS_model3
Attribution['ROS_model4']=ROS_model4
Attribution['ROS_model5']=ROS_model5

Attribution.to_csv('./OUTS/Attribution_'+experiment+'-'+contribution+'_'+output_name+'_radio'+str(radio)+'.csv')

print('Number of fires: {}'.format(Attribution.index.size))

